<template>
	<view class="preview">
		<swiper circular :current="classifyStore.currentIndex - 1" @change="handleChange">
			<swiper-item v-for="item in classifyStore.classifyList" :key="item._id">
				<image @click="showMask = !showMask" :src="item.smallPicurl.replace('_small.webp', '.jpg')" mode="aspectFill">
				</image>
			</swiper-item>
		</swiper>

		<!-- 遮罩 -->
		<view class="mask" v-show="showMask">
			<view class="go-back" :style="{top: getStatusBarHeight() + 'px', marginLeft: getIconLeft() + 'px'}"
				@click="handleGoback">
				<uni-icons type="back" color="#fff" size="21"></uni-icons>
			</view>
			<view class="datetime">
				<view class="count">{{classifyStore.currentIndex}}/{{classifyStore.classifyList.length}}</view>
				<view class="time">
					<uni-dateformat :date="Date.now()" format="hh:mm"></uni-dateformat>
				</view>
				<view class="date">
					<uni-dateformat :date="Date.now()" format="MM月dd日"></uni-dateformat>
				</view>
			</view>
			<view class="footer">
				<view class="box" @click="showInfo">
					<uni-icons type="info" size="23"></uni-icons>
					<view class="text">信息</view>
				</view>
				<view class="box" @click="handleClickScore">
					<uni-icons type="star" size="23"></uni-icons>
					<view class="text">评分</view>
				</view>
				<view class="box" @click="handleDownload">
					<uni-icons type="download" size="23"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>

		<!-- 信息的底部弹出层 -->
		<uni-popup ref="infoPopup">
			<view class="info-popup">
				<view class="pop-header">
					<!-- 垂直方向滑动 -->
					<view></view>
					<view class="title">壁纸信息</view>
					<view class="close" @click="handleInfoClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y>
					<view class="content">
						<view class="row">
							<view class="label">壁纸ID : </view>
							<text selectable>{{ currentPic._id }}</text>
						</view>
						<view class="row">
							<view class="label">发布者 :</view>
							<text selectable>{{ currentPic.nickname  }}</text>
						</view>
						<view class="row">
							<view class="label">评分 :</view>
							<view class="ratebox">
								<uni-rate :max="5" :value="currentPic.score" readonly />
								<view class="text">{{ currentPic.score }}分</view>
							</view>
						</view>
						<view class="row">
							<view class="label">摘要 :</view>
							<text selectable>{{ currentPic.description }}</text>
						</view>
						<view class="row">
							<view class="label">标签 :</view>
							<view class="tag" v-for="tag in currentPic.tabs" :key="tag">
								{{ tag }}
							</view>
						</view>
						<view class="infomation">
							声明:本图片来用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，邮箱513894357@q9.com，管理将删除侵权壁纸维护您的权益。
						</view>
					</view>
				</scroll-view>
				<view class="safe-area-inset-bottom"></view>
			</view>
		</uni-popup>
		<!-- 评分弹框 -->
		<uni-popup :safe-area="false" ref="scorePopup" :is-mask-click="false">
			<view class="info-popup score">
				<view class="pop-header">
					<!-- 垂直方向滑动 -->
					<view></view>
					<view class="title">壁纸评分</view>
					<view class="close" @click="handleScoreClose">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<view class="pop-content">
					<uni-rate v-model="userScore"></uni-rate>
					<text>{{ userScore }}分</text>
				</view>
				<view class="pop-footer">
					<button type="default" @click="submitScore" size="mini" :disabled="userScore == 0" plain>确认评分</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		computed,
		ref
	} from 'vue'
	import {
		getStatusBarHeight,
		getIconLeft
	} from '@/utils/system.js'
	import {
		reqDetailWall,
		reqSetScore,
		reqDownloadWall
	} from '@/apis/classify.js'
	import useClassifyStore from '@/pinia/modules/classify.js'

	let classifyStore = useClassifyStore()
	// 是否显示遮罩层
	let imgList = ref([])
	let currentIndex = ref(1)
	let showMask = ref(true)
	let infoPopup = ref()
	let scorePopup = ref()
	let userScore = ref(0)
	let previewUrl = ref('')
	let picId = ref('')

	let currentPic = computed(() => {
		return classifyStore.classifyList[classifyStore.currentIndex - 1]
	})

	const handleDownload = () => {
		console.log('保存图片到相册')
		let filePath = currentPic.value.smallPicurl.replace('_small.webp', '.jpg')
		console.log(filePath)
		// #ifdef  H5
		uni.showModal({
			content: '请长按图片进行保存',
			showCancel: false
		})
		// #endif

		// #ifndef H5
		uni.showLoading({
			title: '下载中',
			mask: true
		})
		uni.getImageInfo({
			src: filePath,
			success: (res) => {
				uni.saveImageToPhotosAlbum({
					filePath: res.path,
					success: async() => {
						uni.showToast({
							title: '下载成功',
							icon: 'none'
						})
						// 同步
						await reqDownloadWall({
							classid: currentPic.value.classid,
							wallId: currentPic.value._id
						})
					},
					fail: err => {
						if (err.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
							uni.showToast({
								title: '保存失败,请重新点击下载',
								icon: 'none'
							})
							return
						}
						uni.showModal({
							title: '提示',
							content: '需要授权保存相册',
							success: res => {
								if (res.confirm) {
									uni.openSetting({
										success: (setting) => {
											if (setting.authSetting['scope.writePhotosAlbum']) {
												uni.showToast({
													title: '获取授权成功',
													icon: 'none'
												})
											} else {
												uni.showToast({
													title: '授权失败',
													icon: 'none'
												})
											}
										}
									})
								}
							}
						})
					},
					complete: () => {
						uni.hideLoading()
					}
				})
			},
		})
		// #endif
	}

	// 处理滑块滑动
	const handleChange = (event) => {
		classifyStore.setCurrentIndex(event.detail.current + 1)
	}
	// 信息展示
	const showInfo = () => {
		infoPopup.value.open('bottom')
	}
	// 信息关闭
	const handleInfoClose = () => {
		infoPopup.value.close()
	}
	// 点击评分, 开始弹框
	const handleClickScore = () => {
		scorePopup.value.open()
	}
	// 开始评分
	const submitScore = async () => {
		await reqSetScore({
			classid: currentPic.value.classid,
			wallId: currentPic.value._id,
			userScore: userScore.value
		})
		// 提示评分成功
		uni.showToast({
			title: '已评分',
			icon: 'none'
		})
		scorePopup.value.close()
	}
	const handleScoreClose = () => {
		scorePopup.value.close()
	}
	// 返回上一级
	const handleGoback = () => {
		console.log('返回上一级')
		uni.navigateBack({
			success: () => {
				console.log('yifanhui')
			},
			fail: () => {
				uni.reLaunch({
					url: '/pages/index/index'
				})
			}
		})
	}

	onLoad(({
		index
	}) => {
		classifyStore.setCurrentIndex(index)
	})
</script>

<style lang="scss" scoped>
	.preview {
		width: 100%;
		height: 100vh;
		position: relative;

		swiper {
			height: 100%;

			image {
				height: 100%;
				width: 100%;
			}
		}

		.mask {
			top: 0;
			left: 0;

			.go-back {
				position: absolute;
				left: 40rpx;
				cursor: pointer;
				// border: 1px solid #efefef;
				border-radius: 50%;
				width: 60rpx;
				height: 60rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background-color: rgba(0, 0, 0, 0.5);
				backdrop-filter: blur(10px);
				z-index: 100;
			}

			.datetime {
				position: absolute;
				left: 0;
				right: 0;
				top: 6vh;
				margin: 0 auto;

				.count {
					color: #fff;
					font-size: 28rpx;
					background: rgba(0, 0, 0, 0.2);
					letter-spacing: 6rpx;
					padding: 8rpx 28rpx;
					margin: 0 auto;
					width: fit-content;
					border-radius: 40rpx;
					backdrop-filter: blur(10rpx);
				}

				.time {
					width: fit-content;
					font-size: 140rpx;
					letter-spacing: 5rpx;
					margin: 0 auto;
					color: #fff;
					margin-top: 20rpx;
				}

				.date {
					width: fit-content;
					font-size: 30rpx;
					color: #fff;
					margin: 0 auto;
					margin-top: 10rpx;
				}
			}

			.footer {
				position: absolute;
				left: 0;
				right: 0;
				width: 580rpx;
				margin: 0 auto;
				bottom: 200rpx;
				background: rgba(255, 255, 255, 0.8);
				display: flex;
				justify-content: space-around;
				align-items: center;
				border-radius: 80rpx;
				color: #444;
				height: 110rpx;
				backdrop-filter: blur(10rpx);

				.box {
					cursor: pointer;
					width: 150rpx;
					text-align: center;

					:deep(.uni-icons) {
						color: #444 !important;
					}

					.text {
						font-size: 28rpx;
						color: $text-font-color-2;
					}
				}
			}
		}

		.info-popup {
			background-color: #fff;
			padding: 30rpx;
			border-top-left-radius: 10rpx;
			border-top-right-radius: 10rpx;

			&.score {
				width: 70vw;
				border-radius: 10rpx;
			}

			.pop-header {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.title {
					color: $text-font-color-2;
					font-size: 26rpx;
				}

				.close {
					padding: 6px;
				}
			}

			.pop-content {
				text-align: center;
				display: flex;
				align-items: center;
				justify-content: center;
				margin: 30rpx 0 50rpx 0;

				text {
					margin-left: 20rpx;
					color: $text-font-color-2;
				}
			}

			.pop-footer {
				text-align: center;
			}

			scroll-view {
				max-height: 60vh;

				.content {
					.row {
						display: flex;
						align-items: center;
						padding: 16rpx 0;
						font-size: 32rpx;
						line-height: 1.7em;

						.label {
							color: $text-font-color-3;
							text-align: right;
							width: 140rpx;
							font-size: 30rpx;
							margin-right: 15rpx;
							justify-self: baseline;
						}

						.tag {
							width: fit-content;
							padding: 0rpx 16rpx;
							border-radius: 40rpx;
							border: 1px solid $brand-theme-color;
							color: $brand-theme-color;
							font-size: 23rpx;
							margin-right: 20rpx;
						}

						text {
							flex: 1;
							width: 0;
							font-size: 31rpx;
							color: $text-font-color-1;
						}

						.ratebox {
							display: flex;
							align-items: center;

							.text {
								margin-left: 10rpx;
								font-size: 26rpx;
								color: rgb(255, 202, 62);
								transform: translateY(4rpx);
							}
						}
					}

					.infomation {
						margin-top: 20rpx;
						padding: 20rpx;
						background-color: #efefef;
						border-radius: 10rpx;
						color: $text-font-color-2;
						font-size: 30rpx;
						line-height: 50rpx;
					}
				}
			}
		}
	}
</style>